﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using nStruct;
using nFile;
using nGrid;
using nUtil;
namespace LDS1000_V1
{
	public partial class frmSetup : Form
	{
		//frmMain fMain;
		private int setup_group = -1;		// 선택된 그룹의 정보 
		PS_Conf_Struct Setup_Conf = new PS_Conf_Struct();
		PS_Conf_Struct Setup_Conf_temp = new PS_Conf_Struct();
		public string[] ErrorList;
		public frmSetup(Setup_Group setup_group)
		{
			//인덱스 값 그냥 찍어본거 MessageBox.Show("" + setup_group.GROUP);
			this.setup_group = setup_group.GROUP;
			InitializeComponent();
		}

		private void frmSetup_Load(object sender, EventArgs e)
		{
			frmMain fMain = this.Owner as frmMain;
			cUtil.LDS1000_Structure_Copy(ref Setup_Conf, fMain.PS_Conf[setup_group]);
			cUtil.LDS1000_Structure_Copy(ref Setup_Conf_temp, fMain.PS_Conf[setup_group]);
			

			cmbPSNo.Items.Clear();
			for (int i = 0; i < 40; i++) cmbPSNo.Items.Add((i + 1).ToString());
			//cmbPSNo.SelectedIndex = 0;

			cmbTempNo.Items.Clear();
			for (int i = 0; i < 16; i++) cmbTempNo.Items.Add((i + 1).ToString());
			//cmbTempNo.SelectedIndex = 15;



			if (Setup_Conf.FileName == "")
			{
				PS_Default_Value_Setup();
				Temp_Default_Value_Setup();
			}
			Setup_Data_Write();

		}

		
		private void frmSetup_FormClosed(object sender, FormClosedEventArgs e)
		{
			
			if (Setup_Closed_Process() != 0) return;
			frmMain fMain = this.Owner as frmMain;
				
			
			if ( cUtil.LDS1000_Structure_Compare(Setup_Conf_temp,Setup_Conf)==false)
			{
				DialogResult dr = MessageBox.Show("Data 변경되었습다. 반영하시겠습니다.", "Data 변경", MessageBoxButtons.YesNo);
				if(dr == DialogResult.Yes)
				{
					cUtil.LDS1000_Structure_Copy(ref fMain.PS_Conf[setup_group], Setup_Conf);
					fMain.PS_Conf[setup_group].Calc_HW_Data(fMain.PS_Conf[setup_group]);
					fMain.Object_Create();
				}
 			}
			fMain.Enabled = true;
		}



		private void closeToolStripMenuItem_Click(object sender, EventArgs e)
		{
			cGrid.SetupForm_Temp_Grid_Read(gridTemp, ref Setup_Conf);
			cGrid.SetupForm_PS_Grid_Read(gridPS, ref Setup_Conf);
			if (Setup_Closed_Process() == 0)
			{
				this.Close();
			}
		}

		private void btnCreate_Click(object sender, EventArgs e)
		{
			PS_Create_Value_Setup();
			cGrid.SetupForm_PS_Grid_Create(gridPS, Setup_Conf);
		}

		
		private void PS_Default_Value_Setup()
		{
			Setup_Conf.PsNo = 40;
			Setup_Conf.VsetMaster = 100;
			Setup_Conf.IsetMaster = 200;
			Setup_Conf.IProtect_Master = 220;
			PS_Value_Setup();
		}
		private void PS_Create_Value_Setup()
		{
			Setup_Conf.PsNo = Convert.ToInt32(cmbPSNo.Text);
			Setup_Conf.VsetMaster = Convert.ToSingle(numVoltage.Value);
			Setup_Conf.IsetMaster = Convert.ToSingle(numCurrent.Value);
			Setup_Conf.IProtect_Master = Convert.ToSingle(txtProtectUpper.Text);

			PS_Value_Setup();
		}
		private void PS_Value_Setup()
		{
			for (int i = 0; i < Setup_Conf.PsNo; i++)
			{
				Setup_Conf.PsEn[i] = 1;
				Setup_Conf.StringNo[i] = 1;
				Setup_Conf.PsName[i] = "PS " + (i + 1).ToString();
				Setup_Conf.Vset[i] = Setup_Conf.VsetMaster;
				Setup_Conf.Iset[i] = Setup_Conf.IsetMaster;
				Setup_Conf.IProtect[i] = Setup_Conf.IProtect_Master;
			}
		}

		private void Temp_Default_Value_Setup()
		{
			Setup_Conf.TempNo = 16;
			Setup_Conf.TempProtectMaster = 80;

			Temp_Value_Setup();
		}
		private void Temp_Create_Value_Setup()
		{
			Setup_Conf.TempNo = Convert.ToInt32(cmbTempNo.Text);
			Setup_Conf.TempProtectMaster = Convert.ToByte(txtTempProtectMaster.Text);

			Temp_Value_Setup();
		}
		private void Temp_Value_Setup()
		{
			for (int i = 0; i < Setup_Conf.TempNo; i++)
			{
				Setup_Conf.TempEn[i] = 1;
				Setup_Conf.TempName[i] = "Temp " + (i + 1).ToString();
				Setup_Conf.TempProtect[i] = Setup_Conf.TempProtectMaster;
			}
		}
		

	
		private void numCurrent_ValueChanged(object sender, EventArgs e)
		{
			Setup_Conf.IsetMaster = Convert.ToSingle(numCurrent.Value);
		}

		private void numVoltage_ValueChanged(object sender, EventArgs e)
		{
			Setup_Conf.VsetMaster = Convert.ToSingle(numVoltage.Value);
		}


		private void btnTempCreate_Click(object sender, EventArgs e)
		{
			Temp_Create_Value_Setup();
			cGrid.SetupForm_Temp_Grid_Create(gridTemp, Setup_Conf);
		}

		

		

		private void openToolStripMenuItem_Click(object sender, EventArgs e)
		{
			openDlg.Title = "LDS1000 Power Supply Setup File Open";
			openDlg.Filter = "(*.psc)|*.psc";
			openDlg.FileName = "";
			openDlg.ShowDialog();

			if (openDlg.FileName != "")
			{
				Setup_Conf=cFile.PowerSetup_FileOpen(openDlg.FileName);
				Setup_Data_Write();
			}
		}

		private void saveToolStripMenuItem_Click(object sender, EventArgs e)
		{

			saveDlg.Title = "LDS1000 Power Supply Setup File Save";
			saveDlg.Filter = "(*.psc)|*.psc";
			saveDlg.FileName = "";
			saveDlg.ShowDialog();

			if (saveDlg.FileName != "")
			{
				Setup_Data_Read();
				Setup_Conf.FullFileName = saveDlg.FileName;
				cFile.PowerSetup_FileSave(Setup_Conf);

			}
		}
		private void Setup_Data_Read()
		{

			cGrid.SetupForm_PS_Grid_Read(gridPS, ref Setup_Conf);
			cGrid.SetupForm_Temp_Grid_Read(gridTemp, ref Setup_Conf);
			
			
		}

		private void Setup_Data_Write()
		{

			cmbPSNo.Text = Setup_Conf.PsNo.ToString();
			numVoltage.Value = Convert.ToDecimal(Setup_Conf.VsetMaster);
			numCurrent.Value = Convert.ToDecimal(Setup_Conf.IsetMaster);
			txtProtectUpper.Text = Convert.ToString(Setup_Conf.IProtect_Master);

			
			cGrid.SetupForm_PS_Grid_Create(gridPS, Setup_Conf);
			
			cmbTempNo.SelectedIndex = Setup_Conf.TempNo - 1;
			txtTempProtectMaster.Text = Convert.ToString(Setup_Conf.TempProtectMaster);
			
			cGrid.SetupForm_Temp_Grid_Create(gridTemp, Setup_Conf);
		}


		private void gridPS_CurrentCellChanged(object sender, EventArgs e)
		{

			if (gridPS == null) return;
			if (gridPS.CurrentCellInfo.ColIndex==1)
			{
				int row, col;
				row = gridPS.CurrentCellInfo.RowIndex;
				col = gridPS.CurrentCellInfo.ColIndex;
				if (gridPS[row, col].Text == "1")
				{
					for (int i = 2; i < gridPS.ColCount; i++)
					{
						gridPS[row, 1 + i].BackColor = Color.White;
						gridPS[row, 1 + i].CellType = "TextBox";
					}

				}
				else
				{
					for (int i = 2; i < gridPS.ColCount; i++)
					{
						gridPS[row, 1 + i].BackColor = Color.Gray;
						gridPS[row, 1 + i].CellType = "Static";
					}
				}

			}
			
			 
		}

		private void gridTemp_CurrentCellChanged(object sender, EventArgs e)
		{
			if (gridTemp == null) return;
			if (gridTemp.CurrentCellInfo.ColIndex == 1)
			{
				int row, col;
				row = gridTemp.CurrentCellInfo.RowIndex;
				col = gridTemp.CurrentCellInfo.ColIndex;
				if (gridTemp[row, col].Text == "1")
				{
					for (int i = 1; i < gridTemp.ColCount; i++)
					{
						gridTemp[row, 1 + i].BackColor = Color.White;
						gridTemp[row, 1 + i].CellType = "TextBox";
					}

				}
				else
				{
					for (int i = 1; i < gridTemp.ColCount; i++)
					{
						gridTemp[row, 1 + i].BackColor = Color.Gray;
						gridTemp[row, 1 + i].CellType = "Static";
					}
				}
			}
		}

		private int Setup_Closed_Process()
		{
			cGrid.SetupForm_PS_Grid_Read(gridPS, ref Setup_Conf);
			cGrid.SetupForm_Temp_Grid_Read(gridTemp, ref Setup_Conf);
			ErrorList = Find_OK_Reault();

			if (ErrorList[0] != "O.K")
			{
				frmError fError = new frmError();
				fError.Show(this);
				return 1;//OK error
			}
			return 0;//OK

		}

		private string[] Find_OK_Reault()
		{
			string[] ErrStr=new string[100];
			int err=0;
			//1 Power Supply Number(string No)
			//2 동일 Name 확인
			//3 Vset가 150V 이상인가?
			//4 Iset가 500mA 이상인가?
			//5 Warning Lower가 Iset보다 큰가
			//6 Warning Upper가 Iset보다 작은가
			//7 Protect Lower가 Iset보다 큰가
			//8 Protect Upper가 Iset보다 작은가
			//9 Warning Lower가 Protect Lower 보다 큰가
			//10 Warning Upper가 Protect Uppwer 보다 큰가

			//1 Power Supply Number(string No)
			int sumPS = 0;
			for (int i = 0; i < Setup_Conf.PsNo; i++)
			{
				sumPS += Setup_Conf.StringNo[i];
			}
			if (sumPS > 40) ErrStr[err++] = "String No.가 40개 이상으로 설정되어 있음";


			//2 동일 Name 확인
			string nName = "";
			int SameCnt=0;
			for(int i=0;i<Setup_Conf.PsNo; i++)
			{
				SameCnt=0;
				nName = Setup_Conf.PsName[i];
				for (int j = 0; j < Setup_Conf.PsNo; j++)
				{
					if (nName == Setup_Conf.PsName[j]) SameCnt++;
				}
				if (SameCnt > 1)
				{//error
					ErrStr[err++] = "<동일한 PS Name 설정> " + nName;
					break;
				}
			}

			//3 Vset가 150V 이상인가?
			for (int i = 0; i < Setup_Conf.PsNo; i++)
			{
				if (Setup_Conf.Vset[i] > 150)
				{
					ErrStr[err++] = "<150V 이상 설정> " +  Setup_Conf.PsName[i]+" : "+Setup_Conf.Vset[i].ToString();
				}
			}
			//4 Iset가 500mA 이상인가?
			for (int i = 0; i < Setup_Conf.PsNo; i++)
			{
				if (Setup_Conf.Iset[i] > 500)
				{
					ErrStr[err++] = "<500mA 이상 설정> " + Setup_Conf.PsName[i] + " : " + Setup_Conf.Iset[i].ToString();
				}
			}

		
			//8 Protect Upper가 Iset보다 작은가
			for (int i = 0; i < Setup_Conf.PsNo; i++)
			{
				if (Setup_Conf.Iset[i] > Setup_Conf.IProtect[i])
				{
					ErrStr[err++] = "<ISet 보다 Protect Upper 값이 큼> " + Setup_Conf.PsName[i] + " : " + Setup_Conf.IProtect[i].ToString();
				}
			}

			string[] Ret;
			if (err == 0)
			{
				Ret = new string[1];
				Ret[0] = "O.K";
			}
			else
			{
				Ret = new string[err];
				for(int i=0;i<err;i++) Ret[i]=ErrStr[i];
			}

			return Ret;
		}

		private void gridPS_CurrentCellAcceptedChanges(object sender, CancelEventArgs e)
		{
			cGrid.SetupForm_PS_Grid_ErrorCheck(gridPS);
		}

		private void gridTemp_CurrentCellAcceptedChanges(object sender, CancelEventArgs e)
		{
			cGrid.SetupForm_Temp_Grid_ErrorCheck(gridTemp);
		}
	}
}

